package com.parenting.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.parenting.model.IMPage;
import com.parenting.oauth2.config.AuthUtil;
import com.parenting.request.ActivityCouponConsumptionListRequest;
import com.parenting.request.CouponAddRequest;
import com.parenting.request.CouponListSearchCriteriaRequest;
import com.parenting.request.CouponUpdateRequest;
import com.parenting.response.ActivityCouponListResponse;
import com.parenting.response.CouponDetails;
import com.parenting.response.CouponListResponse;
import com.parenting.service.CouponService;
import com.parenting.util.base.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 优惠券 前端控制器
 * </p>
 *
 * @author 烟台业达-王世玺
 * @since 2021-08-27
 */
@RestController
@Api(tags = "活动管理-优惠券管理", description = "优惠券的增删改查")
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService;
    /***
     * 新增优惠券
     * @param couponAddRequest 新增优惠券的对象
     */
    @ApiOperation("新增优惠券")
    @PostMapping
    public Result addCoupon(@RequestBody @Validated CouponAddRequest couponAddRequest){
        couponService.addCoupon(couponAddRequest, AuthUtil.getUserInfo().getShopId());
        return  Result.success();
    }

    /***
     * 用于 优惠券的 修改
     * @param request
     */

    @ApiOperation("修改店铺")
    @PutMapping
    public Result updateCouponById(@RequestBody @Validated  CouponUpdateRequest request){
        couponService.updateCouponById(request);
        return  Result.success();
    }

    /****
     * 优惠券分页列表
     * @param request
     * @return
     */
    @ApiOperation("查看优惠券列表")
    @PostMapping("list")
    public Result<  IMPage<CouponListResponse> > getPageListCoupon(@RequestBody @Validated CouponListSearchCriteriaRequest request){
        IMPage<CouponListResponse> pageListCoupon = couponService.getPageListCoupon(request, AuthUtil.getUserInfo().getShopId());
        return  Result.buildSuccess(pageListCoupon);
    }


    /***
     * 活动优惠券使用情况列表
     * @param request 活动优惠券使用情况查询条件对象
     * @return
     */
    @ApiOperation("查看活动优惠券使用情况列表")
    @PostMapping("consumption")
    public Result<IMPage<ActivityCouponListResponse>>  getActivityCouponConsumptionList(@RequestBody @Validated ActivityCouponConsumptionListRequest request){
        IPage<ActivityCouponListResponse> activityCouponConsumptionList = couponService.getActivityCouponConsumptionList(AuthUtil.getUserInfo().getShopId(), request);
        IMPage<ActivityCouponListResponse> imPage = BeanUtil.copyProperties(activityCouponConsumptionList, IMPage.class);
        return  Result.buildSuccess(imPage);
    }



    @ApiImplicitParam(name = "couponId", value = "优惠券ID", required = true)
    @ApiOperation("查看活动优惠券使用情况列表详情")
    @GetMapping("{couponId}")
    public Result<CouponDetails> getCouponByCouponId(@PathVariable("couponId") String couponId){
        return Result.buildSuccess(couponService.getCouponByCouponId(couponId));
    }


}

